package pluginUtils;

import sliceformUtils.CoupleDAngles;
import sliceformUtils.DroiteSpace3D;
import sliceformUtils.Encombrant;
import sliceformUtils.Rotable;

/** A circle in 3D space is defined with : 
 * - center
 * - radius
 * (which define together a 2D circle)
 * - A plan where it lies. 
 * @author decomite
 *
 */
public class Circle3D implements Rotable<Circle3D>{
	protected Pos3D center;
	protected double rayon; 
	protected Plane plan; 
	
	public Circle3D (Pos3D c,double r,Plane p){
		this.center=c; 
		this.plan=p; 
		this.rayon=r;
	}

	
	public static Circle3D  buildCircle3D(Pos3D p1,Pos3D p2,Pos3D p3){
		Plane p=new Plane(p1,p2,p3); 
		
		// Milieu de p1,p2
		Pos3D m1=Pos3D.middle(p1,p2); 
		Pos3D vecP1P2=Pos3D.sub(p2,p1);
		Pos3D direction1=Pos3D.produitvectoriel(vecP1P2, p.getVecteurNormal()); 
		DroiteSpace3D biss1_2=new DroiteSpace3D(m1, direction1); 
		
		// Milieu de p1,p3
		Pos3D m2=Pos3D.middle(p1,p3); 
		Pos3D vecP1P3=Pos3D.sub(p3,p1);
		Pos3D direction2=Pos3D.produitvectoriel(vecP1P3, p.getVecteurNormal()); 
		DroiteSpace3D biss1_3=new DroiteSpace3D(m2, direction2); 
		
		Pos3D center=DroiteSpace3D.intersection(biss1_2,biss1_3); 
		/*
		System.out.println("distance point centre "+Pos3D.distance(center, p1)); 
		System.out.println("distance point centre "+Pos3D.distance(center, p2)); 
		System.out.println("distance point centre "+Pos3D.distance(center, p3)); 
		
		System.out.println("distance plan centre dans sp "+p.relative(center)); 
		*/
		
		// Rayon
		double rayon=Pos3D.distance(p1, center); 
		
		return new Circle3D(center, rayon, p); 
	}
	public Pos3D getCenter() {
		return center;
	}

	public Plane getPlan() {
		return plan;
	}
	
	public double getRayon(){
		return rayon; 
	}

	public String toString(){
		String s=""; 
		s+=" Plan : "+this.plan+"\n rayon :  "+this.rayon+"\n centre "+this.center; 
		return s; 
	}
	
	public String rawString(){
		String s="";
		s+=this.center.rawString()+" "+this.rayon+" "+this.plan.rawString(); 
		return s; 
		
		
		
	}


	@Override
	public Circle3D rotate(CoupleDAngles cda) {
		Pos3D newCenter=this.center.rotate(cda); 
		Plane newPlan=this.plan.rotate(cda);
		return new Circle3D(newCenter,this.rayon,newPlan); 
	}


	@Override
	public Circle3D rotateInverse(CoupleDAngles cda) {
		Pos3D newCenter=this.center.rotateInverse(cda); 
		Plane newPlan=this.plan.rotateInverse(cda);
		return new Circle3D(newCenter,this.rayon,newPlan); 
	}
}
